package com.gk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eight.pojo.TbIntegrationHistory;
import com.eight.pojo.TbMemberLevel;
import com.eight.pojo.TbUser;
import com.eight.util.CustomerUtil;
import com.eight.util.TimeUtil;
import com.eight.vo.ComResult;
import com.eight.vo.TbUserVo;
import com.eight.vo.UserChartVo;
import com.gk.dao.TbIntegrationHistoryDao;
import com.gk.dao.TbMemberLevelDao;
import com.gk.dao.TbUserDao;
import com.gk.service.TbUserService;
import org.aspectj.weaver.ast.Var;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: GK
 * @date: 2023/2/27 10:30
 * @version:No1.0
 */
@Service(value = "tbUserServiceImpl")
public class TbUserServiceImpl extends ServiceImpl<TbUserDao, TbUser> implements TbUserService {
    @Resource
    private TbUserDao tbUserDao;

    @Resource
    private TbIntegrationHistoryDao tbIntegrationHistoryDao;


    @Resource
    private TbMemberLevelDao tbMemberLevelDao;
    /**
     * 根据条件分页查询所有用户
     *
     * @param pageIndex 当前页
     * @param pageSize  一页中的内容数量
     * @return 返回查询到的用户数据
     */
    @Override
    public ComResult selectAll(TbUserVo tbUserVo, Integer pageIndex, Integer pageSize) {
        Page<TbUser> page = new Page<>(pageIndex, pageSize);
        page = tbUserDao.selectLIkeUser(page, tbUserVo);
        if (page != null) {
            return new ComResult(2000, "查询成功", page);
        }
        return new ComResult(5000, "查询失败");
    }

    /**
     * 根据账号和密码查询用户信息
     *
     * @param password 密码
     * @param username 账户
     * @return 返回客户信息
     */
    @Override
    public TbUser selectOne(String username, String password) {
        QueryWrapper<TbUser> wapper = new QueryWrapper<>();
        wapper.eq("username", username);
        wapper.eq("password", password);
        return tbUserDao.selectOne(wapper);
    }

    /**
     * 根据用户id删除用户数据
     *
     * @param id 用户id
     * @return 返回前端数据
     */
    @Override
    public ComResult deleteFindId(Integer id) {
        int i = tbUserDao.deleteById(id);
        if (i > 0) {
            return new ComResult(2000, "删除成功");
        }
        return new ComResult(5000, "删除失败");
    }

    /**
     * 修改用户信息
     *
     * @param tbUser 需要修改的用户信息
     * @return 返回前端信息
     */
    @Override
    public ComResult updateUser(TbUser tbUser) {
        int i = tbUserDao.updateById(tbUser);
        if (i > 0) {
            return new ComResult(2000, "修改成功");
        }
        return new ComResult(5000, "修改失败");
    }

    /**
     * 添加用户信息
     *
     * @param tbUser 用户信息
     * @return 响应前端的信息
     */
    @Override
    public ComResult insertUser(TbUser tbUser) {
        int insert = tbUserDao.insert(tbUser);
        if (insert > 0) {
            return new ComResult(2000, "添加成功");
        } else {
            return new ComResult(5000, "添加失败");
        }
    }

    /**
     * 查询用户信息
     *
     * @param id 查询条件
     * @return 查询的用户信息
     */
    @Override
    public ComResult selectFindId(Integer id) {
        TbUser tbUser = tbUserDao.selectById(id);
        if (tbUser != null) {
            return new ComResult(2000, "查询成功", tbUser);
        } else {
            return new ComResult(5000, "查询失败");
        }
    }

    /**
     * 查询所有用户
     *
     * @return 查询到的用户
     */
    @Override
    public List<TbUser> selectUserAll() {
        return tbUserDao.selectList(null);
    }

    /**
     * 根据用户账号查询用户信息
     *
     * @param username 账号
     * @return 查询到的用户信息
     */
    @Override
    public TbUser selectUserFindUsername(String username) {
        if ("".equals(username)) {
            return null;
        } else {
            QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
            wrapper.eq("username", username);
            return tbUserDao.selectOne(wrapper);
        }
    }


    /**
     * 获取用户地址和数量
     *
     * @return 用户的地址和数量
     */
    @Override
    public Map<String, Object> getUserList() {
        List<UserChartVo> userChartVos = tbUserDao.selectUserList();
        List<String> address = new ArrayList<>();
        List<Integer> number = new ArrayList<>();
        for (UserChartVo userChartVo : userChartVos) {
            address.add(userChartVo.getLabels());
            number.add(userChartVo.getData());
        }
        Map<String, Object> map = new HashMap<>();
        map.put("address", address);
        map.put("number", number);
        return map;
    }

    /**
     * 根据用户和密码进行添加用户
     *
     * @param username 账号
     * @param password 密码
     * @return 响应前端
     */
    @Override
    public ComResult insertOneUser(String username, String password) {
        TbUser tbUser = new TbUser();
        tbUser.setPassword(password);
        tbUser.setUsername(username);
        tbUser.setAddress("暂无");
        int i = tbUserDao.insert(tbUser);
        if (i > 0) {
            return new ComResult(2000, "注册成功");
        } else {
            return new ComResult(5000, "注册失败");
        }
    }


    /**
     * 判断手机号是否存在
     *
     * @param username 手机号既是账号
     * @return 响应前端
     */
    @Override
    public ComResult selectUsername(String username) {
        QueryWrapper<TbUser> user = new QueryWrapper<>();
        user.eq("username", username);
        TbUser tbUser = tbUserDao.selectOne(user);
        if (tbUser == null) {
            return new ComResult(2000, "手机号未注册，允许注册");
        } else {
            return new ComResult(5000, "手机号已注册");
        }
    }

    /**
     * 根据账号和密码修改密码
     *
     * @param username 账号
     * @param password 密码
     * @return 响应前端
     */
    @Override
    public ComResult updateOneUser(String username, String password) {
        QueryWrapper<TbUser> users = new QueryWrapper<>();
        users.eq("username", username);
        TbUser tbUser = new TbUser();
        tbUser.setUsername(username);
        tbUser.setPassword(password);
        int update = tbUserDao.update(tbUser, users);
        if (update > 0) {
            return new ComResult(2000, "修改成功");
        } else {
            return new ComResult(5000, "修改失败");
        }
    }

    /**
     * 根据账号查询余额
     *
     * @param username 账号
     * @return 响应前端
     */
    @Override
    public TbUser selectTbUser(String username) {
        QueryWrapper<TbUser> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("username", username);
        TbUser tbUser = tbUserDao.selectOne(objectQueryWrapper);
        if (tbUser != null) {
            return tbUser;
        } else {
            return null;
        }
    }

    /**
     * 根据账户查询账户余额
     *
     * @param username 账号
     * @return 响应前端数据
     */
    @Override
    public ComResult selectMoney(String username) {
        QueryWrapper<TbUser> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("username", username);
        TbUser tbUser = tbUserDao.selectOne(objectQueryWrapper);
        if (tbUser != null) {
            return new ComResult(2000, "查询成功", tbUser);
        } else {
            return new ComResult(5000, "查询失败");
        }
    }

    /**
     * 根据用户名来更改充值金额
     *
     * @param amount      充值费用
     * @param name        账户
     * @param integration 积分
     * @return 响应前端
     */
    @Override
    public ComResult addMoney(Integer amount, String name, Integer integration) {
        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", name);
        TbUser tbUser = tbUserDao.selectOne(wrapper);
        tbUser.setAccountBalance(tbUser.getAccountBalance() + amount);
        tbUser.setIntegration(tbUser.getIntegration() + integration);
        //会员积分变更记录
        TbIntegrationHistory tbIntegrationHistory = new TbIntegrationHistory();
        tbIntegrationHistory.setMemberId(tbUser.getId());
        tbIntegrationHistory.setCreateTime(TimeUtil.now(""));
        tbIntegrationHistory.setChangeType(1);
        tbIntegrationHistory.setChangeCount(integration);
        tbIntegrationHistory.setOperateNote("充值添加积分");
        tbIntegrationHistory.setSourceType(1);
        //添加积分记录
        tbIntegrationHistoryDao.insert(tbIntegrationHistory);
        //修改用金额
        int update = tbUserDao.update(tbUser, wrapper);

        if (update > 0) {
            return new ComResult(2000, "充值成功");
        } else {
            return new ComResult(5000, "充值失败");
        }
    }

    /**
     * 根据账号修改头像
     *
     * @param username 账号
     * @param headPic  头像
     * @return 响应前端
     */
    @Override
    public ComResult updateHeadPic(String username, String headPic) {
        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        TbUser tbUser = tbUserDao.selectOne(wrapper);
        tbUser.setHeadPic(headPic);
        int update = tbUserDao.update(tbUser, wrapper);
        if (update > 0) {
            return new ComResult(2000, "修改成功");
        } else {
            return new ComResult(5000, "修改失败");
        }

    }

    /**
     * 根据账号查询用户信息
     *
     * @param username 账号
     * @return 响应前端
     */
    @Override
    public ComResult getuserinfo(String username) {
        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        TbUser tbUser = tbUserDao.selectOne(wrapper);
        if (tbUser != null) {
            return new ComResult(2000, "查询成功", tbUser);
        } else {
            return new ComResult(5000, "查询失败");
        }
    }


    /**
     * 根据账号查询账户余额，并进行修改
     *
     * @param username 账户id
     * @param money    账户
     * @return
     */
    @Override
    public int updateUserMoney(String username, Double money) {
        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        TbUser tbUser = tbUserDao.selectOne(wrapper);
        double by = tbUser.getAccountBalance();
        if (money <= by) {
            //查询所有会员等级
            List<TbMemberLevel> tbMemberLevels = tbMemberLevelDao.selectList(null);
            double acco = by - money;
            tbUser.setAccountBalance(acco);
            tbUser.setGrowth(tbUser.getGrowth()+money);
            TbMemberLevel level = CustomerUtil.getLevel(tbMemberLevels, tbUser.getGrowth());
            tbUser.setMemberLevelId(level.getId());
            return tbUserDao.update(tbUser, wrapper);
        } else {
            return 0;
        }
    }

    /**
     * 校验密码
     *
     * @param username 账号
     * @param password 密码
     * @return 返回是否成功
     */
    @Override
    public ComResult selectPassword(String username, String password) {
        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        TbUser tbUser = tbUserDao.selectOne(wrapper);
        if (password.equals(tbUser.getPassword())) {
            return new ComResult(2000, "密码正确");
        } else {
            return new ComResult(5000, "密码错误");
        }
    }

    /**
     * 修改密码
     *
     * @param username 账号
     * @param password 密码
     * @return 1
     */
    @Override
    public ComResult updatePassword(String username, String password) {
        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        TbUser tbUser = tbUserDao.selectOne(wrapper);
        tbUser.setPassword(password);
        int i = tbUserDao.updateById(tbUser);
        if (i > 0) {
            return new ComResult(2000, "修改成功", i);
        } else {
            return new ComResult(5000, "修改失败");
        }
    }

    /**
     * 完善客户信息
     *
     * @param tbUserVo 完善信息内容
     * @return 1
     */
    @Override
    public ComResult updateUserOver(TbUserVo tbUserVo) {
        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", tbUserVo.getUsername());
        TbUser tbUser1 = tbUserDao.selectOne(wrapper);
        tbUser1.setIdType(tbUserVo.getIdType());
        tbUser1.setContactAddress(tbUserVo.getContactAddress());
        tbUser1.setContactName(tbUserVo.getContactName());
        tbUser1.setPhoneNum(tbUserVo.getPhoneNum());
        tbUser1.setLicenseNo(tbUserVo.getLicenseNo());
        tbUser1.setSex(tbUserVo.getSex());
        tbUser1.setContactPhone(tbUserVo.getContactPhone());
        tbUser1.setEmail(tbUserVo.getEmail());
        tbUser1.setIdCode(tbUserVo.getIdCode());
        tbUser1.setPathFront(tbUserVo.getPathFront());
        tbUser1.setPathReverse(tbUserVo.getPathReverse());
        tbUser1.setName(tbUserVo.getName());
        int update = tbUserDao.updateUser(tbUser1);
        if (update > 0) {
            return new ComResult(2000, "修改成功");
        } else {
            return new ComResult(5000, "修改成功");
        }
    }

}